package com.bitcola.security.admin.biz;

import com.bitcola.security.admin.entity.ColaConfig;
import com.bitcola.security.admin.mapper.ColaConfigMapper;
import com.bitcola.security.admin.vo.ColaConfigVO;
import com.bitcola.security.common.msg.AppResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @date 2019/11/20 15:15
 */
@Service
public class ColaAppBiz {

    @Autowired
    ColaConfigMapper colaConfigMapper;



    public AppResponse<ColaConfigVO> list() {
        List<ColaConfig> colaConfigs = colaConfigMapper.selectAppVersion();
        List<ColaConfigVO> vos = new ArrayList<>();
//        现在安卓版本，再找iOS版本
        for (int i = 0; i < colaConfigs.size(); i++) {
            if ("android_version".equals(colaConfigs.get(i).getConfig())) {
                ColaConfigVO colaConfigVO = new ColaConfigVO();
                BeanUtils.copyProperties(colaConfigs.get(i), colaConfigVO);
                for (i++; i < colaConfigs.size(); i++) {
                    if ("android_url".equals(colaConfigs.get(i).getConfig())) {
                        colaConfigVO.setDownloadAddr(colaConfigs.get(i).getValue());
                        vos.add(colaConfigVO);
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < colaConfigs.size(); i++) {
            if ("ios_version".equals(colaConfigs.get(i).getConfig())) {
                ColaConfigVO colaConfigVO = new ColaConfigVO();
                BeanUtils.copyProperties(colaConfigs.get(i), colaConfigVO);
                for (i++; i < colaConfigs.size(); i++) {
                    if ("ios_url".equals(colaConfigs.get(i).getConfig())) {
                        colaConfigVO.setDownloadAddr(colaConfigs.get(i).getValue());
                        vos.add(colaConfigVO);
                        break;
                    }
                }
            }
        }
        return AppResponse.ok().data(vos);
    }

    @Transactional
    public AppResponse update(ColaConfigVO colaConfigVO) {
        if (StringUtils.isEmpty(colaConfigVO.getId())) {
            return new AppResponse<>(500, "数据错误");
        }
//        找版本号，找下载地址
        ColaConfig oldVersionConfig = colaConfigMapper.selectByPrimaryKey(colaConfigVO.getId());
        oldVersionConfig.setValue(colaConfigVO.getValue());
        colaConfigMapper.updateByPrimaryKey(oldVersionConfig);
//        找下载地址
        String configValue = "";
        if (oldVersionConfig.getConfig().equals("android_version")) {
            configValue = "android_url";
        } else if (oldVersionConfig.getConfig().equals("ios_version")) {
            configValue = "ios_url";
        }
        ColaConfig oldUrlConfig = colaConfigMapper.selectUrlByConfig(configValue);
        oldUrlConfig.setValue(colaConfigVO.getDownloadAddr());
        colaConfigMapper.updateByPrimaryKey(oldUrlConfig);
        return AppResponse.ok();
    }
}
